package com.fit.pages.user;

import java.util.List;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.annotations.AdminAccess;
import com.fit.dao.user.UserDAO;
import com.fit.entities.User;
import com.fit.pages.ErrorPage;

@AdminAccess
public class ManageUsers {

	@Property
	private List<User> users;
	
	@Property
	private User user;
	
	@Inject
	private UserDAO userDAO;
	
	@InjectPage
	private ErrorPage errorPage;
	
	void onActivate() {
		users = userDAO.findAll();
	}
	
	@OnEvent(component = "deleteLink")
	Object onDeleteLink(String username) {
		try {
			userDAO.deleteByUsername(username);
			errorPage.setMessage("User with username " + username + " sucesfully deleted from DB");
			return null;
		} catch (Exception e) {
			errorPage.setMessage("Error");
			return null;
		}
	}
	
}
